package fr.cnam.projetSiw.widget;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;

import fr.cnam.projetSiw.client.ressources.GeneralBundle;
import fr.cnam.projetSiw.shared.Artist;
import fr.cnam.projetSiw.shared.MusicEvent;
import fr.cnam.projetSiw.widget.ressources.WidgetBundle;

public class PhotoGrid extends FlexTable
{

	private int column = 0;
	private int row = 0;
	
	public PhotoGrid()
	{
		super();
		this.addStyleName(WidgetBundle.INSTANCE.cssGrid().photoGrid());
	}
	
	public FocusPanel addCase(MusicEvent e)
	{
		AbsolutePanel container = new AbsolutePanel();

		FocusPanel focus = new FocusPanel();
		container.add(focus);
		container.addStyleName(WidgetBundle.INSTANCE.cssGrid().container());
		//TODO gros caca
		container.getElement().getStyle().setBackgroundImage("url("+ e.getEventImg() + ")");
		this.setWidget(row, column, container);
//		Image img = new Image(a.getImg());
//		container.add(img);
		
		
		
//		container.addStyleName(WidgetBundle.INSTANCE.css().bgImg());
		
		
		FlowPanel subPanel = new FlowPanel();
		container.add(subPanel);
		subPanel.addStyleName(WidgetBundle.INSTANCE.cssGrid().subPanel());	
		subPanel.addStyleName(WidgetBundle.INSTANCE.cssGrid().bgGris());
		
		
		
		HorizontalPanel pnlNumber = new HorizontalPanel();
		pnlNumber.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		HTML eventName = new HTML(e.getName());
		//nom trop long cca fiat moche sur deux lignes
		if(e.getName().length() >24)
		{
			eventName.setText(eventName.getText().substring(0, 23) + "..");
			eventName.setTitle(e.getName());
		}
		HorizontalPanel pnlArtistes = new HorizontalPanel();
		pnlArtistes.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		HorizontalPanel pnlGenre = new HorizontalPanel();
		HTML subscribers = new HTML(e.getNbParticipant() + " ");
		HTML subText = new HTML("participants à ");
		
		pnlNumber.setSpacing(3);
		pnlArtistes.setSpacing(3);
		pnlNumber.addStyleName(WidgetBundle.INSTANCE.cssGrid().pnlNumber());
		pnlArtistes.addStyleName(WidgetBundle.INSTANCE.cssGrid().pnlNumber());
		
		
		pnlNumber.add(eventName);		
		pnlNumber.add(subscribers);
		pnlNumber.add(subText);
		HTML city = new HTML(" " + e.getCity());
		pnlNumber.add(city);
		int cptArtist = 0;
		if(e.getAssociatedArtist() !=null)
		{
			for(Artist a:e.getAssociatedArtist())
			{
				if(cptArtist == 2)
					break;
				if(a.getName() != null && !a.getName().isEmpty())
				{
					HTML lbl = null;
					if(pnlArtistes.getWidgetCount() == 0)
					{
						pnlArtistes.add(new HTML("Avec "));
						lbl = new HTML(" " + a.getName());
						pnlArtistes.add(lbl);
					}
					else
					{
						lbl = new HTML(", " + a.getName());
						pnlArtistes.add(lbl);
					}
					lbl.setTitle(a.getName());
					lbl.addStyleName(WidgetBundle.INSTANCE.cssGrid().link());
					lbl.addStyleName(GeneralBundle.INSTANCE.css().bold());
					lbl.addStyleName(GeneralBundle.INSTANCE.css().nowrap());
					cptArtist++;
				}
				if(a.getGenres() !=null && a.getGenres().size() > 0)
				{
					int cptGenre = 0;
					pnlGenre = new HorizontalPanel(); 
					for(String g:a.getGenres())
					{
						if(cptGenre == 2)
							break;
						if(g == null || g.isEmpty())
						{
							continue;
						}
						HTML genre = new HTML(g);
						genre.setText(" " + genre.getText());
						
						if(cptGenre == 0 && a.getGenres().size() > 1)
						{
							genre.setText(genre.getText() + " - ");
						}
						
						genre.addStyleName(WidgetBundle.INSTANCE.cssGrid().link());
						pnlGenre.add(genre);
						cptGenre++;
					}
					pnlGenre.addStyleName(WidgetBundle.INSTANCE.cssGrid().genre());
				}
				if (a.getName() != null && a.getName().length() > 22)
				{
					break;
				}
				
			}
			
		}
		
		subPanel.add(eventName);		
		subPanel.add(pnlArtistes);
		subPanel.add(pnlNumber);
		subPanel.add(pnlGenre);
		
		eventName.addStyleName(WidgetBundle.INSTANCE.cssGrid().artistName());
		subscribers.addStyleName(WidgetBundle.INSTANCE.cssGrid().subscribers());
		
		
		
	
		
		if(column == 2)
		{
			row++;
			column = 0;
		}
		else 
		{			
			column++;
		}
		focus.setWidth("300px");
		focus.setHeight("312px");
		return focus;
	}
	
	
}
